<!DOCTYPE HTML>
<html>
<head>
    <title>客户表单</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="csrf-token" content="{{ csrf_token() }}"/>
    <link rel="stylesheet" type="text/css" href="/assets/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/assets/easyui/themes/icon.css" />
    <script type="text/javascript" src="/assets/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/assets/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/easyui_expand.js"></script>
    <script type="text/javascript" src="/js/system/customer.js"></script>
    <script type="text/javascript" src="/assets/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/js/utils.js"></script>
    <style>
        #distpicker select{height: 30px;line-height: 30px;
            -webkit-border-radius:3px;
            -moz-border-radius:3px;
            border-radius:3px;}
        .save_customer{    padding: 10px 20px;
            -webkit-border-radius:5px;
            -moz-border-radius:5px;
            border-radius:5px;
            border: 1px solid #eee;
            font-size: 18px;
            cursor: pointer;}
        .save_customer:hover{background-color: #ff5001;border-color: #ff5001;color: #fff;}
    </style>
    <script>
        var RESOURCE_URL = '{{env('RESOURCE_URL')}}';
    </script>
</head>
<body class="easyui-layout">
<div data-options="region:'north',@if(!empty($info) && $info['id'])title:'修改用户' @else title:'新增用户' @endif ,split:true" style="height:340px;">
    <form method="post" enctype="multipart/form-data">
        <table id="add_new_customer">
            <tr>
                <td>客户名称:</td>
                <td><input type="text" class="easyui-textbox" name="name" data-options="required:true" value="@if(!empty($info) && $info['name']){{$info['name'] }} @endif"><input type="hidden" name="id" value="@if(!empty($info) && $info['id']){{$info['id'] }} @endif"></td>

                <td>电话:</td>
                <td><input type="text" class="easyui-textbox" name="tel" data-options="required:true" value="@if(!empty($info) && $info['phone']){{$info['phone'] }} @endif"></td>
                <td>贸易类型:</td>
                <td>
                    <input id="business_type" name="business_type" data-options="required:true"
                           class="easyui-combobox" style="width:173px;">
                </td>
            </tr>
            <tr>
                <td>纳税人识别号:</td>
                <td>
                    <input name="taxid" data-options="required:true" class="easyui-textbox"  value="@if(!empty($info) && $info['taxid']){{$info['taxid'] }} @endif">
                </td>

                <td>开票地址:</td>
                <td><input type="text" data-options="required:true" class="easyui-textbox" name="adds"  value="@if(!empty($info) && $info['adds']){{$info['adds'] }} @endif"></td>
                <td>经营地址:</td>
                <td>
                    <div id="distpicker">
                        <select id="province" data-province="---- 选择省 ----" title="选择省" data-options="required:true"></select>
                        <select id="city" data-city="---- 选择市 ----" title="选择市" data-options="required:true"></select>
                        <select id="district" data-district="---- 选择区 ----" title="选择区" data-options="required:true"></select>
                    </div>
                </td>
            </tr>
            <tr>
                <td>详细地址:</td>
                <td><input type="text" name="address_detail" data-options="required:true" class="easyui-textbox"   value="@if(!empty($info) && $info['address_detail']){{$info['address_detail'] }} @endif"></td>
                <td>开户银行:</td>
                <td><input class="easyui-textbox" data-options="required:true" name="bank"  value="@if(!empty($info) && $info['bank']){{$info['bank'] }} @endif"></td>
                <td>开户银行账号:</td>
                <td><input class="easyui-textbox" data-options="required:true" name="bankAcc"   value="@if(!empty($info) && $info['bankAcc']){{$info['bankAcc'] }} @endif"></td>
            </tr>
            <tr>
                <td>经营状态:</td>
                <td><input class="easyui-combobox" name="business_state" id="business_state"
                           data-options="required:true" style="width: 173px;"></td>
            </tr>
            <tr>
                <td>三证合一:</td>
                <td>
                    <input type="text" name="is_threeInOne" @if(!empty($info) && $info['is_threeInOne']) checked @endif class="easyui-switchbutton" data-options="onText:'是',offText:'否'" />
                </td>

                <td>三证合一证:</td>
                <td><input     style="width:173px;" id="three_licence_elc" accept="image/*" type="file" name="three_licence_elc"/>
                </td>
                <tr>
                <td>营业执照:</td>
                <td>
                    <input  type="file" style="width: 173px"   id="business_licence_number_elc" accept="image/*" name="business_licence_number_elc"/>
                </td>

                <td>组织机构代码:</td>
                <td>
                    <input  type="file" style="width: 173px"    id="organization_code_electronic"   accept="image/*" name="organization_code_electronic"/>
                </td>
                <td>税务登记证号:</td>
                <td>
                    <input  type="file" style="width: 173px"   id="tax_registration_certif_elc"   accept="image/*" name="tax_registration_certif_elc"/>
                </td>
            </tr>
            <tr>
                <td>是否生效:</td>
                <td><input type="text" name="is_active" @if(!empty($info) && $info['is_active']) checked @endif class="easyui-switchbutton" data-options="onText:'是',offText:'否'" /></td>
            </tr>
            <tr>
                <td>描述:</td>
                <td colspan="3"><textarea name="description" style="resize: none;width: 360px;height: 60px;text-indent: 20px;" >@if(!empty($info) && $info['description']) {{$info['description']}} @endif </textarea></td><td colspan="2"><button class="save_customer" onclick="save_customer()">保  存</button></td>
            </tr>
        </table>
    </form>
<form id="add_sales_dialog"></form>

</div>
<div data-options="region:'center',title:'商家联系人信息'" style="padding:5px;background:#eee;height: 320px;">
    <table id="linkmanlist" class="easyui-datagrid" title="" style="width:1000px;height:auto"
           data-options="
				iconCls: 'icon-edit',
				singleSelect: true,
				toolbar: '#linkmanlist_tb',
				url: '/sys/customer/linkmanlist',
				method: 'get',
				queryParams:{_token:'{{csrf_token()}}'},
				onClickRow: onClickRow
			">
        <thead>
        <tr>
            <th data-options="field:'id',width:80,align:'center'">编号</th>
            <th data-options="field:'linkman',width:80,align:'center',editor:{type:'textbox',required:true}">联系人</th>
            <th data-options="field:'mobile',width:100,align:'center',editor:{type:'numberbox',options:{length:11}}">手机号</th>
            <th data-options="field:'phone',width:100,align:'center',editor:'numberbox'">座机</th>
            <th data-options="field:'email',width:120,align:'center',editor:'textbox'">邮箱</th>
            <th data-options="field:'qq',width:90,align:'center',editor:{type:'textbox'}">QQ号</th>
            <th data-options="field:'fax',width:60,align:'center',editor:{type:'textbox'}">传真</th>
            <th data-options="field:'description',width:250,align:'center',editor:{type:'textbox'}">描述</th>
            <th data-options="field:'is_active',width:60,align:'center',editor:{type:'checkbox',options:{on:'P',off:''}}">是否生效</th>
        </tr>
        </thead>
    </table>
    <!--工具栏-->
    <div id="linkmanlist_tb" style="height:auto">
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">增加</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">删除</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">重置</a>
    </div>

</div>
<div data-options="region:'south',title:'客户销售人员信息',split:true" style="height:300px;">
    <table id="customer_sales" class="easyui-datagrid" title="" style="width:1000px;height:auto"
           data-options="
				iconCls: 'icon-edit',
				singleSelect: true,
				toolbar: '#customer_sales_tb',
				url: '/sys/customer/customersales',
				method: 'get',
				queryParams:{_token:'{{csrf_token()}}'},
				onClickRow: onClickRow
			">
        <thead>
        <tr>
            <th data-options="field:'id',width:80,align:'center'">编号</th>
            <th data-options="field:'username',width:80,align:'center'">用户名</th>
            <th data-options="field:'realname',width:100,align:'center'">用户姓名</th>
            <th data-options="field:'sex',width:100,align:'center'">性别</th>
            <th data-options="field:'phone',width:100,align:'center'">手机号</th>
            <th data-options="field:'email',width:120,align:'center'">邮箱</th>
            <th data-options="field:'qq',width:90,align:'center'">QQ号</th>
            <th data-options="field:'fax',width:60,align:'center'">传真</th>
            <th data-options="field:'description',width:90,align:'center'">备注</th>
            <th data-options="field:'is_active',width:60,align:'center'">是否生效</th>
            <th data-options="field:'is_admin',width:80,align:'center'">是否管理员</th>
        </tr>
        </thead>
    </table>
    <!--工具栏-->
    <div id="customer_sales_tb" style="height:auto">
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addSales()">增加</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeSales()">删除</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="acceptSales()">保存</a>
    </div>
</div>
<script type="text/javascript" src="/js/distpicker.min.js"></script>
<script type="text/javascript">
    //当前页面的客户id
    var customer_id = $('input[name=id]').val();
    //保存客户
    function save_customer() {
        //添加图片
        var form = new FormData();
        try {
            form.append('three_licence_elc', $('#three_licence_elc')[0].files[0]);
        } catch (e) {
            //修改时可能不会修改上传图片
        }try {
            form.append('business_licence_number_elc', $('#business_licence_number_elc').filebox('getValue'));
        } catch (e) {
            //修改时可能不会修改上传图片
        }try {
            form.append('organization_code_electronic', $('#organization_code_electronic').filebox('getValue'));
        } catch (e) {
            //修改时可能不会修改上传图片
        }try {
            form.append('tax_registration_certif_elc', $('#tax_registration_certif_elc').filebox('getValue'));
        } catch (e) {
            //修改时可能不会修改上传图片
        }
        var business_state = $('#business_state');
        var business_type = $('#business_type');
        form.append('id', $.trim($('input[name=id]').val()));
        form.append('name', $.trim($('input[name=name]').val()));
        form.append('tel', $.trim($('input[name=tel]').val()));
        form.append('taxid', $.trim($('input[name=taxid]').val()));
        form.append('adds', $.trim($('input[name=adds]').val()));
        form.append('province_id', $('#province').val());
        form.append('city_id', $('#city').val());
        form.append('district_id', $('#district').val());
        form.append('area_info', $('#province option:selected').text()+' '+$('#city option:selected').text()+' '+$('#district option:selected').text());
        form.append('address_detail', $.trim($('input[name=address_detail]').val()));
        form.append('business_state', $.trim(business_state.combobox('getValue')));
        form.append('business_type', $.trim(business_type.combobox('getValues')));
        form.append('bank', $.trim($('input[name=bank]').val()));
        form.append('bankAcc', $.trim($('input[name=bankAcc]').val()));
        form.append('is_threeInOne', $.trim($('input[name=is_threeInOne]').val()));
        form.append('is_active', $.trim($('input[name=is_active]').val()));
        form.append('description', $.trim($('textarea[name=description]').val()));

        $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            url: '/sys/customer/save',
            type: 'post',
            cache: false,
            processData: false,
            data: form,
            dataType: 'json',
            success: function (data) {
                if (data.code == 200) {
                    $.messager.alert("提示信息", '保存成功', "success");
                } else {
                    $.messager.alert("提示信息", data.data, "error");
                }
            }
        });
    }
    var editIndex = undefined;
    var linkmanlist = $('#linkmanlist');
    var customer_sales = $('#customer_sales');
    function endEditing(){
        if (editIndex === undefined){return true}
        if (linkmanlist.datagrid('validateRow', editIndex)){
            var ed = linkmanlist.datagrid('getEditor', {index:editIndex,field:'productid'});
            var productname = $(ed.target).combobox('getText');
            linkmanlist.datagrid('getRows')[editIndex]['productname'] = productname;
            linkmanlist.datagrid('endEdit', editIndex);
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }
    function onClickRow(index){
        if (editIndex !== index){
            if (endEditing()){
                linkmanlist.datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
                editIndex = index;
            } else {
                linkmanlist.datagrid('selectRow', editIndex);
            }
        }
    }
    function append(){
        if (endEditing()){
            linkmanlist.datagrid('appendRow',{status:'P'});
            editIndex = linkmanlist.datagrid('getRows').length-1;
            linkmanlist.datagrid('selectRow', editIndex)
                .datagrid('beginEdit', editIndex);
        }
    }
    function removeit(){
        if (editIndex === undefined){return}
        linkmanlist.datagrid('cancelEdit', editIndex)
            .datagrid('deleteRow', editIndex);
        editIndex = undefined;
    }
    function accept(){
        if (endEditing()){
            linkmanlist.datagrid('acceptChanges');
        }
        if(!!customer_id){

        }else{
            $.messager.alert("提示信息", '请先添加客户信息', "error");
        }
    }
    function reject(){
        linkmanlist.datagrid('rejectChanges');
        editIndex = undefined;
    }
    //添加销售员
    function addSales() {
        $('#add_sales_dialog').dialog({
            title: '选择雇员',
            width: 1100,
            height: 400,
            closed: false,
            cache: false,
            href: '/sys/customer/addcustomer',
            modal: true,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: function () {
                    /*选择用户填充表格*/
                    var rows = $('#userListGrid').datagrid('getSelected');
                    if (null === rows) {
                        $.messager.alert("提示信息", "请选择需要编辑的行", "info");
                        return;
                    }
                    /*插入新行*/
                    $('#customer_sales').datagrid('insertRow',{
                        index: 1,  // 索引从0开始
                        row: {
                            id: rows.id,
                            username: rows.username,
                            realname: rows.realname,
                            sex: rows.sex,
                            phone: rows.phone,
                            email: rows.email,
                            qq: rows.qq,
                            fax: rows.fax,
                            description: rows.description,
                            is_active: rows.is_active,
                            is_admin: rows.is_admin
                        }
                    });
                    $('#add_sales_dialog').dialog('close');
                }
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#add_sales_dialog').dialog('close');
                }
            }]
        });
    }
    //删除销售员
    function removeSales() {
        if (editIndex === undefined){return}
        customer_sales.datagrid('cancelEdit', editIndex)
            .datagrid('deleteRow', editIndex);
        editIndex = undefined;
    }
    //保存销售员
    function acceptSales() {
        if(!!customer_id){

        }else{
            $.messager.alert("提示信息", '请先添加客户信息', "error");
        }
    }
</script>
<script>
    $(function () {
        $("#distpicker").distpicker({
            valueType: "code"
        });
        var province = $("#province");
        var city = $("#city");
        var district = $("#district");
        @if(empty($info['address_provinceid']) || empty($info['address_cityid']) || empty($info['address_areaid']))
        province.trigger("change");
        city.trigger("change");
        district.trigger("change");
        @else
        province.val({{$info['address_provinceid']}}).trigger("change");
        city.val({{$info['address_cityid']}}).trigger("change");
        district.val({{$info['address_areaid']}}).trigger("change");
        @endif
        var token = $('meta[name="csrf-token"]').attr('content');
        $('#business_type').combobox({
            url: '/sys/dict/infolist',
            queryParams: {
                _token: token,
                code: 'business_type'
            },
            valueField: 'id',
            textField: 'label',
            editable: false
        })
        $('#business_state').combobox({
            url: '/sys/dict/infolist',
            queryParams: {
                _token: token,
                code: 'customer_state'
            },
            valueField: 'id',
            textField: 'label',
            editable: false
        })
        @if(!empty($info))
        $('#business_type').combobox('setValue',{{$info['business_type']}});
        $('#business_state').combobox('setValue',{{$info['business_state']}});
        @endif
    });
</script>
</body>
</html>
